#!/bin/bash

if [[ $1 = "clean" ]]; then
	find * ! -name 'init.sh' -exec rm -rf {} +
	exit
fi



project_name=$1
env_name=$2


###########################################################################################
#	FUNCTIONS
###########################################################################################

function create_requirements_txt {
	cat > requirements.txt << EOF
aliyun-python-sdk-core==2.3.5
-e git+https://bitbucket.org/cincore/aliyun-python-sdk-dysmsapi.git@95fbe059876e70dc4bac61df2aaa47886d897a76#egg=aliyun_python_sdk_dysmsapi
appdirs==1.4.3
coreapi==2.3.0
coreschema==0.0.4
Django==1.11
django-filter==1.0.2
django-pure-pagination==0.3.0
django-rest-swagger==2.1.2
djangorestframework==3.6.2
djangorestframework-filters==0.10.0
drf-extensions==0.3.1
easy-thumbnails==2.4.1
itypes==1.1.0
Jinja2==2.9.6
MarkupSafe==1.0
MySQL-python==1.2.5
olefile==0.44
openapi-codec==1.3.1
optionaldict==0.1.1
packaging==16.8
Pillow==4.1.0
pycrypto==2.6.1
pyparsing==2.2.0
pypinyin==0.18.1
python-alipay-sdk==1.2.1
python-dateutil==2.6.0
pytz==2017.2
qrcode==5.3
requests==2.6.0
simplejson==3.10.0
six==1.10.0
unicodecsv==0.14.1
uritemplate==3.0.0
wechat-sdk==0.6.4
wechatpy==1.4.1
xmltodict==0.9.2

EOF

echo "@@@ CREATE requirements.txt FILE"
}

function create_gitignore_txt {
	cat > .gitignore << EOF
# osx noise
.DS_Store
profile

*.log
*.log.*
*.pot
*.py[cod]
*.db

*.pydevproject
.project
.metadata
bin/**
tmp/**
tmp/**/*
*.tmp
*.bak
*.swp
*~.nib
local.properties
.classpath
.settings/
.loadpath
.idea
._*
.sass-cache

# Locally stored "Eclipse launch configurations"
*.launch

*/local_settings.py

*-ck.js

/bower_components
/node_modules
config.codekit
config.codekit3

/static
/media
/certs

db.sqlite3
*.map
data.json
.gitignore

EOF

echo "@@@ CREATE .gitignore FILE"
}

function create_settings_py {
	cat > $project_name/settings.py << EOF
from global_settings import *

try:
    from local_settings import *
except ImportError:
    pass

EOF
echo "@@@ CREATE new settings.py FILE"
}

function create_uwsgi_ini {
	cat > deploy/uwsgi.ini << EOF
[uwsgi]
plugins-dir = /usr/local/lib/uwsgi
plugin = python2
virtualenv = /home/$USER/.virtualenvs/$env_name
chdir = $(pwd)
master = True
vacuum = True
env = DJANGO_SETTINGS_MODULE=$project_name.settings
module = $project_name.wsgi
socket = $(pwd)/deploy/uwsgi.sock
processes = 4
daemonize = $(pwd)/deploy/logs/uwsgi.log
pidfile = $(pwd)/deploy/uwsgi.pid
chmod = 777
enable-threads = true
uid = $USER
stats = $(pwd)/deploy/stats.sock
memory-report = 1
buffer-size=32768

EOF
echo "@@@ CREATE uwsgi.ini FILE"
ln -s $(pwd)/deploy/uwsgi.ini /etc/uwsgi/apps-enabled/$project_name.ini
}

function create_nginx_conf {
	cat > deploy/nginx.conf << EOF
server {

    listen       80;
    server_name  *;


    client_max_body_size 20M;
    access_log $(pwd)/deploy/logs/nginx.access.log;
    error_log $(pwd)/deploy/logs/nginx.error.log error;

    location /static/ {
        alias $(pwd)/static/;
    }

    location /media/ {
        alias $(pwd)/media/;
    }

    location / {
        include uwsgi_params;
        uwsgi_pass unix://$(pwd)/deploy/uwsgi.sock;
        uwsgi_read_timeout 1800;
    }


}


EOF
echo "@@@ CREATE nginx.conf FILE"

ln -s $(pwd)/deploy/nginx.conf /etc/nginx/sites-enabled/$project_name.conf
}

function create_local_settings_py {
	cat > $project_name/local_settings.py << EOF
# -*- coding: utf-8 -*-

import os

DEBUG = True

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

STATIC_ROOT = '$(pwd)/static'
MEDIA_ROOT = '$(pwd)/media'


EOF
	echo "@@@ CREATE local_settings.py FILE"

    if [[ ! -e $project_name/local_settings.py.example ]]; then
        cp $project_name/local_settings.py $project_name/local_settings.py.example
	fi
}



###########################################################################################
#	PROGRAM BODY
###########################################################################################

function init {
	source /usr/local/bin/virtualenvwrapper.sh

	if [ $env_name ]
	then
		workon $env_name
	else
		mkvirtualenv $project_name
	fi

	create_requirements_txt
	create_gitignore_txt
	# pip install -r requirements.txt

	if [ ! -d $project_name ]; then
		django-admin startproject $project_name .
	fi

	mv $project_name/settings.py $project_name/global_settings.py
	echo "@@@ CREATE global_settings.py FILE"
	create_settings_py
	create_local_settings_py
}

function setup {
	if [[ ! -e $project_name/local_settings.py ]]; then
		create_local_settings_py
	fi
	if [[ ! -e .gitignore ]]; then
		create_gitignore_txt
	fi
}

function deploy {
	create_uwsgi_ini
	create_nginx_conf
	cp /etc/nginx/uwsgi_params deploy
}

function enable {
	sudo ln -s $(pwd)/deploy/nginx.conf /etc/nginx/sites-enabled/$project_name.conf
	sudo ln -s $(pwd)/deploy/uwsgi.ini /etc/uwsgi/apps-enabled/$project_name.ini
}

if [[ $1 = "deploy" ]]; then
	shift
	project_name=$1
	env_name=$2

	if [[ ! -d deploy ]]; then
		mkdir deploy
	fi

	if [[ ! -d deploy/logs ]]; then
		mkdir deploy/logs
	fi

	deploy
	exit
fi

if [[ $1 = "enable" ]]; then
	shift
	project_name=$1
	env_name=$2

	if [[ ! -d deploy ]]; then
		echo "deploy dir not found! please check your path"
		exit
	fi

	enable
	exit
fi

if [[ $1 = "setup" ]]; then
	shift
	project_name=$1
	env_name=$2

	setup
	exit
fi


init

